﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.student
{
    public partial class EditPlural : TeacherBasePage
    {
        public IList<DictAreaModel> areaList;
        public Dictionary<int, string> certificateList;
        //public IDictionary<int, string> questionList;
        public IDictionary<int, string> educationList;

        protected override void ShowPage()
        {
            UserBLL bll = new UserBLL();
            //questionList = bll.GetForgetPwdQuestionList();
            educationList = bll.GetEducationList();

            if (ispost)
            {
                string prefix = XRequest.GetString("PrefixTxt");
                int startno = XRequest.GetInt("StartNoTxt", 0);
                int endno = XRequest.GetInt("EndNoTxt", 0);
                int quantity = endno - startno;
                int areaid = XRequest.GetInt("AreaSlt", -1);
                int certificateid = XRequest.GetInt("CertificateSlt", -1);
                //int questionid = XRequest.GetInt("QuestionSlt", -1);
                //string answer = XRequest.GetString("AnswerTxt");
                int educationid = XRequest.GetInt("EducationSlt", -1);

                if (CheckValue())
                {

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < quantity; i++)
                    {
                        sb.Append("'");
                        sb.Append(prefix);
                        sb.Append((startno + i).ToString().PadLeft(endno.ToString().Length, '0'));
                        sb.Append("',");
                    }
                    string checkCondition = sb.ToString();
                    checkCondition = checkCondition.Substring(0, checkCondition.Length - 1);

                    PluralUserModel model = new PluralUserModel();
                    model.Prefix = prefix;
                    model.StartNo = startno;
                    model.EndNo = endno;
                    model.Quantity = quantity;
                    model.Pwd = Utils.MD5(config.DefaultPwd);
                    model.AreaID = areaid;
                    model.CurrentCertificateID = certificateid;
                    model.RegisterTime = DateTime.Now;
                    model.CheckCondition = checkCondition;
                    //model.ForgetPwdQuestionID = questionid;
                    //model.ForgetPwdAnswer = answer;
                    model.ForgetPwdQuestionID = 1;
                    model.ForgetPwdAnswer = "111111";
                    model.EducationID = educationid;

                    if (authority == E.Enum.EnumTeacherAuthority.Internals)
                    {
                        model.IsPublic = false;
                    }
                    else
                    {
                        model.IsPublic = XRequest.GetString("IsPublicSlt") == "1" ? true : false;
                    }


                    int rst = 0;
                    try
                    {
                        rst = bll.AddPluralUsers(model);
                        if (rst > 0)
                        {
                            ExecuteJS("alert('操作成功'); window.location = '/student/EditPlural.aspx';");
                        }
                        else
                        {
                            Alert("操作失败");
                        }
                    }
                    catch (Exception e)
                    {
                        Alert("操作失败");
                    }
                }
            }

            areaList = GetTeacherAreaList(teacherid);
            if (authority == E.Enum.EnumTeacherAuthority.Internals)
            {
                certificateList = (new TeacherBLL()).GetTeacherProjectInfo(teacherid);
            }
            else
            {
                certificateList = (new CourseBLL()).GetAllCertificateAndProjectList();
            }
        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("PrefixTxt").Equals(""))
            {
                Alert("前缀不能为空");
                rst = false;
            }

            if (XRequest.GetInt("StartNoTxt", 0) <= 0)
            {
                Alert("开始编号请大于0");
                rst = false;
            }

            if (XRequest.GetInt("EndNoTxt", 0) <= 0)
            {
                Alert("结束编号请大于0");
                rst = false;
            }

            if (XRequest.GetInt("EndNoTxt", 0) <= XRequest.GetInt("StartNoTxt", 0))
            {
                Alert("结束编号不能小于或等于起始编号");
                rst = false;
            }

            if (XRequest.GetInt("AreaSlt", -1) == -1)
            {
                Alert("请选择报名机构");
                rst = false;
            }

            if (XRequest.GetInt("CertificateSlt", -1) == -1)
            {
                Alert("请选择证书类别");
                rst = false;
            }
            //if (XRequest.GetInt("QuestionSlt", -1) == -1)
            //{
            //    Alert("忘记密码提示问题");
            //    rst = false;
            //}
            //if (XRequest.GetString("AnswerTxt").Equals(""))
            //{
            //    Alert("提示问题答案");
            //    rst = false;
            //}


            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
